package cn.kgc.service;



import cn.kgc.mapper.HouseroleMapper;
import cn.kgc.pojo.Houserole;
import cn.kgc.pojo.HouseroleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HouseroleServiceImpl implements HouseroleService{
    @Resource
    HouseroleMapper houseroleMapper;
    @Override
    public PageInfo<Houserole> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);

        if (keyword==null) {
            List<Houserole> users = houseroleMapper.selectByExample(null);
            PageInfo<Houserole> pageInfo=new PageInfo<>(users);
            return pageInfo;
        }else{
            HouseroleExample example=new HouseroleExample();
            HouseroleExample.Criteria criteria = example.createCriteria();
            //根据姓名查询
            criteria.andHouseroleLike("%"+keyword+"%");
            HouseroleExample.Criteria criteria1 = example.createCriteria();

            List<Houserole> users = houseroleMapper.selectByExample(example);
            PageInfo<Houserole> pageInfo=new PageInfo<>(users);
            return pageInfo;
        }
    }

    @Override
    public Houserole selectByIdUser(Integer id) {
        return houseroleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Houserole> getUserListByIdList(List<Integer> userIdList) {
        HouseroleExample example=new HouseroleExample();
        HouseroleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(userIdList);

        return houseroleMapper.selectByExample(example);
    }

    @Override
    public void batchRemove(List<Integer> userIdList) {
        HouseroleExample example=new HouseroleExample();
        HouseroleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(userIdList);
        houseroleMapper.deleteByExample(example);
    }

    @Override
    public void remove(Integer id) {
        houseroleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Houserole selectById(Integer id) {
        return    houseroleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateUser(Houserole user) {

        houseroleMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void saveUser(Houserole user) {

        houseroleMapper.insert(user);
    }


    @Override
    public List<Houserole> getAssignedRoleList(Integer userId) {
        return houseroleMapper.getAssignedRoleList(userId);
    }

    @Override
    public List<Houserole> getUnAssignedRoleList(Integer userId) {
        return houseroleMapper.getUnAssignedRoleList(userId);
    }

    @Override
    public void updateRelationship(Integer userId, List<Integer> roleIdList) {
        houseroleMapper.deletAdminRelationRole(userId);
        if (roleIdList != null) {
            houseroleMapper.insertAdminRelationRole(userId, roleIdList);
        }
    }
}
